#include "spi.h"

static SPI_HandleTypeDef hspiInstance2;
static SPI_HandleTypeDef hspiInstance1;

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    __HAL_RCC_AFIO_CLK_ENABLE();
    if (hspi->Instance == SPI1)
    {

        __HAL_RCC_SPI1_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();

        GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_6;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
    if (hspi->Instance == SPI2)
    {

        __HAL_RCC_SPI2_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();

        GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_13 | GPIO_PIN_15;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_14;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
}

void MX_SPI1_Init(void)
{
    hspiInstance1.Instance = SPI1;
    hspiInstance1.Init.Mode = SPI_MODE_MASTER;
    hspiInstance1.Init.Direction = SPI_DIRECTION_2LINES;
    hspiInstance1.Init.DataSize = SPI_DATASIZE_8BIT;
    hspiInstance1.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspiInstance1.Init.CLKPhase = SPI_PHASE_1EDGE;
    hspiInstance1.Init.NSS = SPI_NSS_SOFT;
    hspiInstance1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
    hspiInstance1.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspiInstance1.Init.TIMode = SPI_TIMODE_DISABLE;
    hspiInstance1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspiInstance1.Init.CRCPolynomial = 7;
    if (HAL_SPI_Init(&hspiInstance1) != HAL_OK)
    {
        Error_Handler();
    }
}

void MX_SPI2_Init(void)
{
    hspiInstance2.Instance = SPI2;
    hspiInstance2.Init.Mode = SPI_MODE_MASTER;
    hspiInstance2.Init.Direction = SPI_DIRECTION_2LINES;
    hspiInstance2.Init.DataSize = SPI_DATASIZE_8BIT;
    hspiInstance2.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspiInstance2.Init.CLKPhase = SPI_PHASE_1EDGE;
    hspiInstance2.Init.NSS = SPI_NSS_SOFT;
    hspiInstance2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
    hspiInstance2.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspiInstance2.Init.TIMode = SPI_TIMODE_DISABLE;
    hspiInstance2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspiInstance2.Init.CRCPolynomial = 7;
    if (HAL_SPI_Init(&hspiInstance2) != HAL_OK)
    {
        Error_Handler();
    }
}

uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
    uint8_t rxData;
    HAL_SPI_TransmitReceive(&hspiInstance1, &TxData, &rxData, 1, 1000);
    return rxData;
}

uint8_t SPI2_ReadWriteByte(uint8_t TxData)
{
    uint8_t rxData;
    HAL_SPI_TransmitReceive(&hspiInstance2, &TxData, &rxData, 1, 1000);
    return rxData;
}

// //发送指定长度数据
// void SPI2_SendBlock(uint8_t *pData, uint32_t size)
// {
//     volatile char dummy;
//     while (size--)
//     {
//         SPI2->DR = *pData++;
//         while ((SPI2->SR & SPI_FLAG_RXNE) == 0x00)
//             ;
//         dummy = SPI2->DR;
//     }
// }

// //接收指定长度数据
// void SPI2_ReceiveBlock(uint8_t *pData, uint32_t size)
// {
//     while (size--)
//     {
//         SPI2->DR = 0xFF;
//         while ((SPI2->SR & SPI_FLAG_RXNE) == 0x00)
//             ;
//         *pData++ = SPI2->DR;
//     }
// }